package com.heima.freemarker.controller;

import com.heima.freemarker.pojo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * The type Hello controller.
 *
 * @author DiXiaoNie
 * @since 2022 /12/19 15:09 不可以直接写@RestController
 */
@Controller
@RequestMapping("/hello")
public class HelloController {


    /**
     * Index string.
     *
     * @param model the model
     * @return springmvc天然支持freemarker string
     */
    @GetMapping
    public String index(Model model) {

        model.addAttribute("name", "小梁子");

        Student student = new Student();
        student.setName("张三");
        student.setAge(19);

        model.addAttribute("stu", student);
        return "01-basic";
    }


    /**
     * Index string.
     *
     * @param modelMap 作为一个linkedHashMap的子类
     * @return the string
     */
    @GetMapping("/modelMap")
    public String index(ModelMap modelMap) {

        modelMap.put("name", "小梁子1111");

        Student student = new Student();
        student.setName("张三2222");
        student.setAge(19);

        modelMap.put("stu", student);
        return "01-basic";
    }


    @GetMapping("/mav")
    public ModelAndView index() {

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("name", "66666666");

        Student student = new Student();
        student.setName("6666");
        student.setAge(19);

        modelAndView.addObject("stu", student);
        modelAndView.setViewName("01-basic");
        return modelAndView;
    }

    @GetMapping("/list")
    public String list(Model model) {
        Student student1 = new Student();
        student1.setName("111");
        student1.setAge(22);
        student1.setMoney(22);

        Student student2 = new Student();
        student2.setName("2222");
        student2.setAge(333);
        student2.setMoney(333);

        ArrayList<Student> arrayList = new ArrayList<>();
        arrayList.add(student1);
        arrayList.add(student2);

        model.addAttribute("stus", arrayList);

        return "02-list";
    }


    @GetMapping("/map")
    public String map(Model model) {
        Student student1 = new Student();
        student1.setName("111");
        student1.setAge(22);
        student1.setMoney(22);

        Student student2 = new Student();
        student2.setName("2222");
        student2.setAge(333);
        student2.setMoney(333);

        ArrayList<Student> arrayList = new ArrayList<>();
        arrayList.add(student1);
        arrayList.add(student2);

        model.addAttribute("stus", arrayList);

        Map map = new HashMap();

        map.put("stu1", student1);
        map.put("stu2", student2);
        model.addAttribute("stuMap", map);

        return "02-list";
    }
}
